home *** CD-ROM | disk | FTP | other *** search
/ Oxygen Multimedia Graphics 22 / Oxygen Multimedia Graphics 22.iso / pc / System / OX22 / Internal_52_Avoid Sprite.ls < prev    next >
Encoding:
Text File  |  2008-03-12  |  5.5 KB  |  193 lines

  1. property pSprite, pDestination, pPeriod, pOrigTime, pDestTime, pOrigin, pVector, pMoving, pStageBounds, pMaxSpeed, pAvoid, pDistance, pSpeed, pActive, pLimited
  2.  
  3. on getBehaviorDescription me
  4.   return "AVOID SPRITE" & RETURN & RETURN & "Moves one sprite to maintain a set distance from another. " & "Set the channel of the sprite to avoid, the speed the sprite moves to avoid the other sprite, whether the sprite starts avoid automatically (or waits to be told to start avoiding), and whether its movement is limited to the stage area." & RETURN & RETURN & "PARAMETERS:" & RETURN & "* Channel of sprite to avoid" & RETURN & "* Speed in pixels/second" & RETURN & "* Active (avoiding sprite) at start?" & RETURN & "* Limited to stage area?"
  5. end
  6.  
  7. on getBehaviorTooltip me
  8.   return "Makes a sprite automatically move away from another sprite. " & "You can select which sprite to avoid, the distance to maintain, and the speed at which the sprite will move."
  9. end
  10.  
  11. on beginSprite me
  12.   mInitialize(me)
  13. end
  14.  
  15. on prepareFrame me
  16.   mUpdate(me)
  17.   mTrackSprite(me)
  18. end
  19.  
  20. on mCenter vRect
  21.   vHalfWidth = vRect.width / 2
  22.   vHalfHeight = vRect.height / 2
  23.   return point(vRect.left + vHalfWidth, vRect.top + vHalfHeight)
  24. end
  25.  
  26. on mCenterOffset vRect
  27.   vCenter = mCenter(vRect)
  28.   return vCenter - sprite(pSprite).loc
  29. end
  30.  
  31. on mCenteredRect vPoint, vRect
  32.   vWidth = vRect.width
  33.   vHeight = vRect.height
  34.   vHalfWidth = vWidth / 2
  35.   vHalfHeight = vHeight / 2
  36.   vLeft = vPoint.locH - vHalfWidth
  37.   vTop = vPoint.locV - vHalfHeight
  38.   return rect(vLeft, vTop, vLeft + vWidth, vTop + vHeight)
  39. end
  40.  
  41. on mFixedLengthVector vVector, vLength
  42.   vNewVector = vVector * vLength / max(1, mVectorLength(vVector))
  43.   return vNewVector
  44. end
  45.  
  46. on mInitialize me
  47.   pSprite = me.spriteNum
  48.   pMaxSpeed = 1000
  49.   pSpeed = min(pMaxSpeed, max(1, integer(pSpeed)))
  50.   pOrigin = sprite(pSprite).loc + mCenterOffset(sprite(pSprite).rect)
  51.   pDestination = pOrigin
  52.   pOrigTime = the milliSeconds
  53.   pDestTime = pOrigTime + 1
  54.   pPeriod = 1
  55.   pVector = point(0, 0)
  56.   pMoving = 0
  57.   pStageBounds = point((the stage).rect.width, (the stage).rect.height)
  58.   mSetDest(pDestination)
  59. end
  60.  
  61. on mSetDest vDest
  62.   if pActive then
  63.     pDestination = vDest
  64.     pOrigin = sprite(pSprite).loc + mCenterOffset(sprite(pSprite).rect)
  65.     pVector = pDestination - pOrigin
  66.     vDistance = mVectorLength(pVector)
  67.     pPeriod = max(1, 1000 * vDistance / pSpeed)
  68.     pOrigTime = the milliSeconds
  69.     pDestTime = pOrigTime + pPeriod
  70.     pMoving = 1
  71.   end if
  72. end
  73.  
  74. on mStageLimit vRect
  75.   if vRect.left < 0 then
  76.     vOffsetH = -vRect.left
  77.   else
  78.     vOffsetH = min(0, pStageBounds.locH - vRect.right)
  79.   end if
  80.   if vRect.top < 0 then
  81.     vOffsetV = -vRect.top
  82.   else
  83.     vOffsetV = min(0, pStageBounds.locV - vRect.bottom)
  84.   end if
  85.   return offset(vRect, vOffsetH, vOffsetV)
  86. end
  87.  
  88. on mTrackSprite me
  89.   if pActive then
  90.     vSprite = the mouseLoc
  91.     vVector = mVectorAway()
  92.     if mVectorLength(vVector) < pDistance then
  93.       if vVector = point(0, 0) then
  94.         vVector = point((random(2) * 2) - 3, (random(2) * 2) - 3)
  95.       end if
  96.       vDest = mCenter(sprite(pAvoid).rect) + mFixedLengthVector(vVector, pDistance)
  97.       mSetDest(vDest)
  98.     else
  99.       pMoving = 0
  100.     end if
  101.   end if
  102. end
  103.  
  104. on mUpdate me
  105.   if pActive then
  106.     if pMoving then
  107.       if the milliSeconds > pDestTime then
  108.         vRect = mCenteredRect(pDestination, sprite(pSprite).rect)
  109.         if pLimited then
  110.           vRect = mStageLimit(vRect)
  111.         end if
  112.         sprite(pSprite).rect = vRect
  113.         pMoving = 0
  114.       else
  115.         vElapsed = the milliSeconds - pOrigTime
  116.         if vElapsed > (pPeriod / 2) then
  117.           nothing()
  118.         end if
  119.         vMoveVector = pVector * vElapsed / pPeriod
  120.         vRect = mCenteredRect(pOrigin + vMoveVector, sprite(pSprite).rect)
  121.         if pLimited then
  122.           vRect = mStageLimit(vRect)
  123.         end if
  124.         sprite(pSprite).rect = vRect
  125.       end if
  126.     end if
  127.   end if
  128. end
  129.  
  130. on mVectorAway
  131.   return mCenter(sprite(pSprite).rect) - mCenter(sprite(pAvoid).rect)
  132. end
  133.  
  134. on mVectorLength vVector
  135.   vSquare = (vVector.locH * vVector.locH) + (vVector.locV * vVector.locV)
  136.   return sqrt(vSquare)
  137. end
  138.  
  139. on mSetActive me, vBool
  140.   if not (not vBool) = vBool then
  141.     pActive = vBool
  142.     pMoving = pActive
  143.   end if
  144. end
  145.  
  146. on mSetAvoid me, vAvoid
  147.   if sprite(vAvoid).type > 0 then
  148.     pAvoid = vAvoid
  149.   end if
  150. end
  151.  
  152. on mSetDistance me, vDist
  153.   vDist = max(1, min(600, integer(vDist)))
  154.   pDistance = vDist
  155. end
  156.  
  157. on mSetLimited me, vBool
  158.   if not (not vBool) = vBool then
  159.     pLimited = vBool
  160.   end if
  161. end
  162.  
  163. on mSetSpeed me, vSpeed
  164.   vSpeed = max(1, min(pMaxSpeed, integer(vSpeed)))
  165.   pSpeed = vSpeed
  166.   mSetDest(me, pDestination)
  167. end
  168.  
  169. on mToggleActive me
  170.   pActive = not pActive
  171.   pMoving = pActive
  172. end
  173.  
  174. on mToggleLimited me
  175.   pLimited = not pLimited
  176. end
  177.  
  178. on isOKToAttach me, aSpriteType, aSpriteNum
  179.   return aSpriteType = #graphic
  180. end
  181.  
  182. on getPropertyDescriptionList
  183.   vSpriteRect = sprite(the currentSpriteNum).rect
  184.   vRadius = max(vSpriteRect.width, vSpriteRect.height)
  185.   vPDList = [:]
  186.   setaProp(vPDList, #pAvoid, [#comment: "Avoid Channel", #format: #integer, #default: the currentSpriteNum + 1])
  187.   setaProp(vPDList, #pDistance, [#comment: "Distance (10-600 pixels)", #format: #integer, #default: vRadius, #range: [#min: 10, #max: 600]])
  188.   setaProp(vPDList, #pSpeed, [#comment: "Speed (20-1000 pixels/sec)", #format: #integer, #default: 1000, #range: [#min: 20, #max: 1000]])
  189.   setaProp(vPDList, #pActive, [#comment: "Active at start", #format: #boolean, #default: 1])
  190.   setaProp(vPDList, #pLimited, [#comment: "Limited to stage area", #format: #boolean, #default: 1])
  191.   return vPDList
  192. end
  193.